-
-
Notifications
You must be signed in to change notification settings - Fork 2k
[tkinter] Annotate few methods #15273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
stdlib/tkinter/__init__.pyi
Outdated
| @overload | ||
| def wantobjects(self) -> int: ... | ||
| @overload | ||
| def wantobjects(self, wantobjects: int, /) -> None: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least for Python 3.12, these are bools:
Python 3.12.3 (main, Jan 8 2026, 11:30:50) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> tkinter.Tk().wantobjects()
True
| @overload | |
| def wantobjects(self) -> int: ... | |
| @overload | |
| def wantobjects(self, wantobjects: int, /) -> None: ... | |
| @overload | |
| def wantobjects(self) -> bool: ... | |
| @overload | |
| def wantobjects(self, wantobjects: bool, /) -> None: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I haven't checked it yet, but I think I'll need to check all the methods in other versions. In Python 3.14 I get 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found that, although PyArg_ParseTuple using different formats ('|i:wantobjects' in all versoins exclude 3.12 which uses '|p:wantobjects') in different versions, they can always accept int and bool. I'd prefer to return int if one is returned, like this:
if sys.version_info >= (3, 14):
@overload
def wantobjects(self) -> Literal[0, 1]: ...
else:
@overload
def wantobjects(self) -> bool: ...
@overload
def wantobjects(self, wantobjects: Literal[0, 1] | bool, /) -> None: ...what do you think? (the number is returned only starting from 3.14)
| def adderrorinfo(self, msg: str, /) -> None: ... | ||
| def call(self, command: Any, /, *args: Any) -> Any: ... | ||
| def createcommand(self, name: str, func, /): ... | ||
| def createcommand(self, name: str, func: Callable[..., object], /) -> None: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure that func takes arbitrary arguments? Maybe @Akuli, our resident tkinter expert has some insights. We should at least leave a TODO comment to check this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to be a callback that takes two arguments like in createfilehandler. But I decided to leave it like that for now because I wasn't sure about it.
I was only sure that it was Callable because there was a check for it in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a "TODO" comment to this and the other callables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like the arguments are always passed as strings, but I might be missing something. Keyword arguments are never given, because they are not a thing in Tcl.
>>> import tkinter; r = tkinter.Tk(); r.withdraw()
''
>>> r.createcommand("foo", (lambda *args: print(args)))
>>> r.call("foo", 1, 2, 3)
('1', '2', '3')
'None'
>>> r.eval("foo 1 2 3")
('1', '2', '3')
'None'
>>> r.call("foo", [1, 2, 3])
('1 2 3',)
'None'
>>> r.eval("foo [list 1 2 3]")
('1 2 3',)
'None'
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Based on: https://github.com/python/cpython/blob/main/Modules/_tkinter.c